package com.itheima.springmvc;

import javafx.scene.chart.ValueAxis;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author fengqingayng
 * @version 1.0.0
 * @date 2019/7/7 15:53
 * @description
 **/
@RequestMapping(value = "hello2")
@Controller
public class Hello2Controller {


    @RequestMapping(value = "show1")
    public ModelAndView test1() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现");
        return view;
    }

    @RequestMapping(value = "*/show2")
    public ModelAndView test2() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现*");
        return view;
    }

    @RequestMapping(value = "**/show3")
    public ModelAndView test3() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现**");
        return view;
    }

    @RequestMapping(value = "a**/show4")
    public ModelAndView test4() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现a**");
        return view;
    }

    /*占位符映射*/
    @RequestMapping(value = "show5/{name}/{age}")
    public ModelAndView test5(@PathVariable String name, @PathVariable Integer age) {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现" +
                "name" + name + "age" + age);
        return view;
    }

    @RequestMapping(value = "show6")
    public ModelAndView test6() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用热部署启动程序使用热部署启动程序使用热部署启动程序");
        return view;
    }

    @RequestMapping(value = "show7", method = RequestMethod.POST)
    public ModelAndView test7() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现,使用POST请求访问");
        return view;
    }

    @RequestMapping(value = "show8", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView test8() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现,使用POST请求访问");
        return view;
    }

    /*限定请求参数映射*/
    @GetMapping(value = "show9", params = {"name", "age"})
    public ModelAndView test9() {
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", "使用注解的Contorller实现,使用POST请求访问name+age");
        return view;
    }

    @GetMapping(value = "show10")
    public ModelAndView test10(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        StringBuffer sb = new StringBuffer();
        sb.append("request=" + request + "<br>");
        sb.append("response=" + response + "<br>");
        sb.append("session=" + session + "<br>");

        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        view.addObject("result", sb);
        return view;
    }


    @GetMapping(value = "show11")
    public String test11(Model model) {
        model.addAttribute("result", "使用model替代modelandview");
        return "hello";
    }

    @GetMapping(value = "show12/{name}")
    public String test12(Model model, @PathVariable String name) {

        model.addAttribute("result", "使用注解的Contorller实现" + name);
        return "hello";
    }

    @GetMapping(value = "show13")
    public String test13(Model model, @RequestParam(value = "name", required = false) String name) {
        model.addAttribute("result", "使用requestparam注解传递参数" + name);

        return "hello";
    }

    @GetMapping(value = "show14")
    public String test14(Model model, @CookieValue(value = "JSESSIONID") String cookie) {

        model.addAttribute("result", "使用注解的Contorller实现,获取交互是产生的cookie值<br>" + cookie);
        return "hello";
    }

    @GetMapping(value = "show15")
    @ResponseStatus(value = HttpStatus.OK)
    public void test15(@RequestParam(value = "name") String name,
                       @RequestParam(value = "age") Integer age,
                       @RequestParam(value = "isMarry", defaultValue = "false") Boolean isMarry,
                       @RequestParam(value = "income") Double income,
                       @RequestParam(value = "interests") String[] interests) {
        StringBuffer sb = new StringBuffer();
        sb.append("name=" + name);
        sb.append("age=" + age);
        sb.append("isMarry=" + isMarry);
        sb.append("incomne=" + income);
        sb.append("insterests=" + Arrays.toString(interests));
        System.out.println("sb = " + sb);

    }

    @GetMapping(value = "show16")
    @ResponseStatus(value = HttpStatus.OK)
    public String test16(Model model, UserVO userVO) {
        model.addAttribute("result", userVO.getUsers());
        return "hello";
    }

    @GetMapping(value = "show17")
    @ResponseStatus(value = HttpStatus.OK)
    public String test17(Model model) {
        List<User> users=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setUserName("李"+i);
            user.setAge(i);
            user.setName("张"+i);
            users.add(user);
        }
        model.addAttribute("result",users);
        return "hello";
    }


}
